package net.daum.codegen;

import java.util.Map;
import java.util.Set;

import net.daum.codegen.domain.Part;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class CodeGenUtil {
	public final static Map<String, Part> partMap;
	private static final String XML = ".xml";
	private static final String JAVA = ".java";
	private final static Part domainPart = new Part("Domain", "domain", null, JAVA);
	private final static Part servicePart = new Part("Service", "service", null, JAVA);
	private final static Part daoPart = new Part("Dao", "dao", null, JAVA);
	private final static Part controllerPart = new Part("Controller", "controller", null, JAVA);
	private final static Part serviceImplPart = new Part("ServiceImpl", "service.impl", "Service", JAVA);
	private final static Part queryPart = new Part("Query", "dao.impl", null, XML);
	
	private final static Set<String> keywordSet;
	private final static Set<String> invalidCharSet;
	
	static {
		partMap = Maps.newLinkedHashMap();
		partMap.put("controllerPart",  controllerPart);
		partMap.put("servicePart",  servicePart);
		partMap.put("serviceImplPart",  serviceImplPart);
		partMap.put("daoPart",  daoPart);
		partMap.put("queryPart",  queryPart);
		partMap.put("domainPart",  domainPart);	
		
		keywordSet = Sets.newHashSet(
				 "abstract"
				,"assert"
				,"boolean"
				,"break"
				,"byte"
				,"case"
				,"catch"
				,"char"
				,"class"
				,"const"
				,"continue"
				,"default"
				,"do"
				,"double"
				,"else"
				,"enum"
				,"extends"
				,"final"
				,"finally"
				,"float"
				,"for"
				,"goto"
				,"if"
				,"implements"
				,"import"
				,"instanceof"
				,"int"
				,"interface"
				,"long"
				,"native"
				,"new"
				,"package"
				,"private"
				,"protected"
				,"public"
				,"return"
				,"short"
				,"static"
				,"strictfp"
				,"super"
				,"switch"
				,"synchronized"
				,"this"
				,"throw"
				,"throws"
				,"transient"
				,"try"
				,"void"
				,"volatile"
				,"while"
				,"false"
				,"null"
				,"true"				
				);
		
		invalidCharSet = Sets.newHashSet();
		for (int i = 0; i < 10; i++) {
			invalidCharSet.add(String.valueOf(i));
		}
	}
	
	public static String getJavaClassName(String name) {
		return toFirstCharUpperCase(getJavaFieldName(name));
	}
	
	public static String getJavaFieldName(String name) {
		String[] value = name.split("_");
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < value.length; i++) {
			if (i == 0) {
				buffer.append(value[i]);
			} else {
				buffer.append(toFirstCharUpperCase(value[i]));	
			}
		}
		
		String tempName = buffer.toString();
		String firstChar = tempName.substring(0, 1);
		
		if (invalidCharSet.contains(firstChar) ||
			keywordSet.contains(tempName)) {
			tempName = "_" + tempName;	
		}
		
		return tempName;
	}
	
	public static String toFirstCharUpperCase(String name) {
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}
	
	public static String toFirstCharLowerCase(String name) {
		return name.substring(0, 1).toLowerCase() + name.substring(1);
	}
	
	public static String getJavaMethodName(String prefix, String name) {
		return prefix + toFirstCharUpperCase(getJavaFieldName(name));
	}
	
	public static String getJavaMethodName(String name) {
		return toFirstCharUpperCase(getJavaFieldName(name));		
	}

	public static Map<String, Part> getPartmap() {
		return partMap;
	}	
}